// CommandLineHelper Library
// =========================
// License: Microsoft Permissive License (Ms-PL) v1.1
// Author: Rhy Mednick (http://www.rhyverse.net)
// Project Site: http://www.codeplex.com/CommandLineHelper

using System;
using System.Collections.Generic;
using System.Text;

namespace CommandLineHelper.Attributes
{
    /// <summary>
    /// When applied to a property, this attribute instructs the command line helper parsing routine to validate 
    /// that the property value passed in is within the specified set of values. This attribute only applies to 
    /// string values.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class ValidateSetAttribute : Attribute, IValidation
    {
        private bool m_ignoreCase=false;
        private string[] m_validValues;
        private string m_statusMessage = String.Empty;

        /// <summary>
        /// Gets the status message that is set when the IsValid() method is called.
        /// </summary>
        public string StatusMessage
        {
            get
            {
                return m_statusMessage;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating if the property to be validated should be compared to the set 
        /// using a case insensitive comparision.
        /// </summary>
        public bool IgnoreCase
        {
            get { return m_ignoreCase; }
            set { m_ignoreCase = value; }
        }

        /// <summary>
        /// Gets or sets the list of valid values.
        /// </summary>
        public string[] ValidValues
        {
            get { return m_validValues; }
            set { m_validValues = value; }
        }

        /// <summary>
        /// Verifies that the value matches one of the values in the set of valid values.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <returns>True if the value is in the set and false if it isn't.</returns>
        public bool IsValid(object value)
        {
            bool retval = false;

            //this only applies to strings, so convert the value
            string strval = value as string;
            if (!String.IsNullOrEmpty(strval))
            {
                if (this.ValidValues != null && this.ValidValues.Length > 0)
                {
                    foreach (string comparestr in this.ValidValues)
                    {
                        if (string.Compare(comparestr, strval, this.IgnoreCase) == 0)
                        {
                            retval = true;
                            break;
                        }
                    }
                }
            }

            return retval;
        }

    }
}
